const express = require("express");

const router = express.Router();

const addressModel = require("../../model/addressModel");

// 导入生成id的包
const { v4: uuidv4 } = require("uuid");

router.post("/editaddress", async (req, res) => {
    let { address_id, address_name, address_phone, address_province, address_city, address_county, address_detail } = req.body;
    // console.log("10", address_id);
    if (address_id === undefined) {
        address_id = uuidv4();
    }
    // console.log("11", address_id);
    await addressModel
        .edit_address(address_id, address_name, address_phone, address_province, address_city, address_county, address_detail)
        .then((content) => {
            res.send({
                code: 200,
                msg: "修改成功",
            });
        })
        .catch((err) => {
            res.send(err);
        });
});

router.get("/getaddress", async (req, res) => {
    await addressModel
        .get_address()
        .then((content) => {
            res.send(content);
        })
        .catch((err) => {
            res.send(err);
        });
});

router.get("/getaddr", async (req, res) => {
    let { address_id } = req.query;
    await addressModel
        .get_addr(address_id)
        .then((content) => {
            res.send({
                code: 200,
                msg: "获取单挑地址成功",
                content,
            });
        })
        .catch((err) => {
            res.send({
                code: -1,
                msg: "获取单挑地址失败",
                err,
            });
        });
});

router.post("/removeaddress", async (req, res) => {
    let { address_id } = req.body;
    await addressModel
        .remove_addres(address_id)
        .then((content) => {
            res.send({
                code: 200,
                msg: "删除地址成功",
            });
        })
        .catch((err) => {
            res.send({
                code: -1,
                msg: "删除地址失败",
                err,
            });
        });
});

module.exports = router;
